package 临时工具.红圈通;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.io.IOException;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Map;

public class getCustomerUtil {
    //项目列表uel
    //工单uel
    private static String customerUrl = "https://mm.hecom.cn/mobile-0.0.1-SNAPSHOT/rcm/e/rcment_5989001/customer/queryCustomerContactsForPC.do";
    private static Map<String, String> headers = HttpUtil.buildHecomHeaders(
            "HTML5_rcmuser10742704_1748223330478_Otlw",
            "15268169505",
            "rcmuser10742704",
            "15268169505"
    );
    private static Statement stmt;
    static {
        try {
            stmt = DBUtil.getConnection().createStatement();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    public static void main(String[] args) throws IOException,SQLException {
        new getCustomerUtil().getCustomer();
    }

    private void getCustomer() throws IOException,SQLException {
    String params = "userStr={\"pageSize\":1500,\"pageIndex\":1,\"searchText\":\"\",\"isStatistics\":1}";
    String response = HttpUtil.postFormWithHeaders(customerUrl, headers, params);

        // 解析 JSON 字符串为 JSONObject
        JSONObject jsonObject = JSONObject.parseObject(response);
        // 获取 "data" 字段（JSONArray）
        JSONArray dataArray = jsonObject.getJSONArray("data");
            dataArray.stream().forEach(e -> {
                JSONObject item = (JSONObject) e;
                String name = item.getString("name");
                String phone = item.getString("phone");
                String customerName = item.getString("customerName");

                System.out.println(name + " " + phone + " " + customerName);
                try {
                    stmt.executeUpdate(StrUtil.format(
                            "insert into hqt_customer(name,phone,customerName) values('{}','{}','{}')",
                            name,
                            phone,
                            customerName
                    ));
                } catch (SQLException ex) {
                    throw new RuntimeException(ex);
                }
            });
    }
    //后续要执行的sql
    //UPDATE `hqt_customer` set phone=REPLACE(phone,' ','');
    //UPDATE `hqt_customer` set phone=REPLACE(phone,'？','');
    //UPDATE `hqt_customer` set phone=REPLACE(phone,'-','');
    //
    //update ops_task t
    //set customer_name=(select name from (select customerName,GROUP_CONCAT(name)name,GROUP_CONCAT(phone)phone from hqt_customer GROUP BY customerName)t where customerName=task_name ),
    //customer_phone=(select phone from (select customerName,GROUP_CONCAT(name)name,GROUP_CONCAT(phone)phone from hqt_customer GROUP BY customerName)t where customerName=task_name)
    // where t.project_id= 100000 and t.task_name in ( select customerName from hqt_customer)
    //
}
